import 'package:flutter/material.dart';
import 'package:fl_chart/fl_chart.dart';
import '../constants/app_colors.dart';
import '../models/mood.dart';
import '../utils/date_util.dart';

class MoodChart extends StatelessWidget {
  final List<Mood> moods;
  final String title;
  final int days;

  const MoodChart({
    Key? key,
    required this.moods,
    required this.title,
    this.days = 7,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (moods.isEmpty) {
      return _buildEmptyState(context);
    }

    // 按日期排序
    final sortedMoods = List<Mood>.from(moods);
    sortedMoods.sort((a, b) => a.date.compareTo(b.date));

    // 获取最近days天的数据
    final recentMoods = sortedMoods.length > days
        ? sortedMoods.sublist(sortedMoods.length - days)
        : sortedMoods;

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          title,
          style: Theme.of(context).textTheme.titleLarge,
        ),
        const SizedBox(height: 16),
        SizedBox(
          height: 200,
          child: LineChart(
            _buildLineChartData(recentMoods, context),
          ),
        ),
      ],
    );
  }

  Widget _buildEmptyState(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          title,
          style: Theme.of(context).textTheme.titleLarge,
        ),
        const SizedBox(height: 16),
        SizedBox(
          height: 200,
          child: Center(
            child: Text(
              '暂无数据',
              style: Theme.of(context).textTheme.bodyLarge?.copyWith(
                color: AppColors.textSecondary,
              ),
            ),
          ),
        ),
      ],
    );
  }

  LineChartData _buildLineChartData(List<Mood> moods, BuildContext context) {
    final spots = moods.asMap().entries.map((entry) {
      final index = entry.key.toDouble();
      final mood = entry.value;
      return FlSpot(index, mood.moodType.toDouble());
    }).toList();

    return LineChartData(
      gridData: FlGridData(
        show: true,
        drawVerticalLine: true,
        horizontalInterval: 1,
        verticalInterval: 1,
        getDrawingHorizontalLine: (value) {
          return FlLine(
            color: Colors.grey.shade200,
            strokeWidth: 1,
          );
        },
        getDrawingVerticalLine: (value) {
          return FlLine(
            color: Colors.grey.shade200,
            strokeWidth: 1,
          );
        },
      ),
      titlesData: FlTitlesData(
        show: true,
        rightTitles: AxisTitles(
          sideTitles: SideTitles(showTitles: false),
        ),
        topTitles: AxisTitles(
          sideTitles: SideTitles(showTitles: false),
        ),
        bottomTitles: AxisTitles(
          sideTitles: SideTitles(
            showTitles: true,
            reservedSize: 30,
            interval: 1,
            getTitlesWidget: (value, meta) {
              if (value.toInt() >= 0 && value.toInt() < moods.length) {
                final date = DateUtil.parseDate(moods[value.toInt()].date);
                return Padding(
                  padding: const EdgeInsets.only(top: 8.0),
                  child: Text(
                    '${date.month}/${date.day}',
                    style: TextStyle(
                      color: AppColors.textSecondary,
                      fontSize: 12,
                    ),
                  ),
                );
              }
              return const SizedBox();
            },
          ),
        ),
        leftTitles: AxisTitles(
          sideTitles: SideTitles(
            showTitles: true,
            interval: 1,
            getTitlesWidget: (value, meta) {
              String text = '';
              switch (value.toInt()) {
                case 1:
                  text = '很差';
                  break;
                case 2:
                  text = '较差';
                  break;
                case 3:
                  text = '一般';
                  break;
                case 4:
                  text = '较好';
                  break;
                case 5:
                  text = '很好';
                  break;
              }
              return Text(
                text,
                style: TextStyle(
                  color: AppColors.getMoodColor(value.toInt()),
                  fontSize: 12,
                ),
                textAlign: TextAlign.left,
              );
            },
            reservedSize: 40,
          ),
        ),
      ),
      borderData: FlBorderData(
        show: true,
        border: Border.all(color: Colors.grey.shade300),
      ),
      minX: 0,
      maxX: moods.length - 1.0,
      minY: 0.5,
      maxY: 5.5,
      lineBarsData: [
        LineChartBarData(
          spots: spots,
          isCurved: true,
          color: AppColors.primary,
          barWidth: 3,
          isStrokeCapRound: true,
          dotData: FlDotData(
            show: true,
            getDotPainter: (spot, percent, barData, index) {
              final moodType = spot.y.toInt();
              return FlDotCirclePainter(
                radius: 6,
                color: AppColors.getMoodColor(moodType),
                strokeWidth: 2,
                strokeColor: Colors.white,
              );
            },
          ),
          belowBarData: BarAreaData(
            show: true,
            color: AppColors.primary.withOpacity(0.1),
          ),
        ),
      ],
    );
  }
} 